package travelDream.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;

import traveldream.ejb.DTO.*;
import traveldream.ejb.interfaces.*;

@ManagedBean(name = "PackageIM")
@ViewScoped
public class PackageInsertionManaged {
	private List<HotelDTO> hDTO;
	private int selectedH;
	private HotelDTO h;
	
	private List<TransportationDTO> gDTO;
	private TransportationDTO g;
	private int selectedG;
	
	private List<TransportationDTO> rDTO;
	private int selectedR;
	private TransportationDTO r;
	
	private List<ExcursionDTO> eDTO;
	private int selectedE;
	private ExcursionDTO e;
	
	private PackageDTO pDTO;
	
	private boolean prima = true;
	
	@EJB
	private PackageInsertionBeanLocal PackageInsertionBeanLocal;
	
	public PackageInsertionManaged(){
		pDTO = new PackageDTO();
	}
	
	public String onFlowProcess(FlowEvent event) {  
        System.out.println(event.getOldStep()+event.getNewStep());
    
        
		if(event.getOldStep().equals("going")){
			for(TransportationDTO sel: gDTO){
				if(sel.getId() == selectedG){
					g = sel;
				}
			}
			rDTO = PackageInsertionBeanLocal.allTransport();
			selectedR = rDTO.get(0).getId();
		}else if(event.getOldStep().equals("return")){
			for(TransportationDTO sel: rDTO){
				if(sel.getId() == selectedR){
					r = sel;
				}
			}
			hDTO = PackageInsertionBeanLocal.allHotel();
			selectedH = hDTO.get(0).getId();
		}else if(event.getOldStep().equals("hotel")){
			for(HotelDTO sel: hDTO){
				if(sel.getId() == selectedH){
					h = sel;
				}
			}
			eDTO = PackageInsertionBeanLocal.allExcursion();
			//selectedE = eDTO.get(0).getId();
		}
		else if(event.getOldStep().equals("excursion")){
			for(ExcursionDTO sel: eDTO){
				if(sel.getId() == selectedE){
					e = sel;
				}
			}
			save();
			
		}
        
        return event.getNewStep();  
    }  
	
	public void allList(){
		if(prima){
			gDTO = PackageInsertionBeanLocal.allTransport();
			selectedG = gDTO.get(0).getId();
			
			prima = false;
		}
	}
	
	public long dayBetweenTwoDate(Date uno, Date due) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(uno);
		c2.setTime(due);

		long giorni = (c2.getTime().getTime() - c1.getTime()
				.getTime())	/ (24 * 3600 * 1000);

		return giorni;
	}

	public void save(){
		pDTO.setIdGoing(selectedG);
		pDTO.setIdReturn(selectedR);
		pDTO.setIdExcursion(selectedE);
		pDTO.setIdHotel(selectedH);
		if(e!=null)
			pDTO.setPrice(h.getPrice()*(int)(dayBetweenTwoDate(g.getArrivalDate(),r.getDepartureDate())) + e.getPrice() + g.getPrice() + r.getPrice());
		else
			pDTO.setPrice(h.getPrice()*(int)(dayBetweenTwoDate(g.getArrivalDate(),r.getDepartureDate())) + g.getPrice() + r.getPrice());
	}
	
	public String create(){
		try{
			System.out.println("prima dei check: " + pDTO.getPrice());
		if (PackageInsertionBeanLocal.checkPackage(selectedH,selectedE,selectedG,selectedR)){
			if(PackageInsertionBeanLocal.checkPrice(selectedH,selectedE,selectedG,selectedR,pDTO)){
				PackageInsertionBeanLocal.save(pDTO);
				return "package.xhtml?faces-redirect=true";
			}
			else{ //you can't have a package that costs more than the sum of its basic product
				
				//notify the error
				 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The price doesn't match with the basic products"));  
			
			}
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The dates and the cities don't match"));  
		}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, go back and try again!!"));  
		}
		return "";
	}
	
	public List<HotelDTO> gethDTO() {
		return hDTO;
	}

	public void sethDTO(List<HotelDTO> hDTO) {
		this.hDTO = hDTO;
	}

	public List<ExcursionDTO> geteDTO() {
		return eDTO;
	}

	public void seteDTO(List<ExcursionDTO> eDTO) {
		this.eDTO = eDTO;
	}

	public int getSelectedH() {
		return selectedH;
	}

	public void setSelectedH(int selectedH) {
		this.selectedH = selectedH;
	}

	public int getSelectedR() {
		return selectedR;
	}

	public void setSelectedR(int selectedR) {
		this.selectedR = selectedR;
	}

	public int getSelectedE() {
		return selectedE;
	}

	public void setSelectedE(int selectedE) {
		this.selectedE = selectedE;
	}

	public PackageDTO getpDTO() {
		return pDTO;
	}

	public void setpDTO(PackageDTO pDTO) {
		this.pDTO = pDTO;
	}

	public int getSelectedG() {
		return selectedG;
	}

	public void setSelectedG(int selectedG) {
		this.selectedG = selectedG;
	}

	public List<TransportationDTO> getgDTO() {
		return gDTO;
	}

	public void setgDTO(List<TransportationDTO> gDTO) {
		this.gDTO = gDTO;
	}

	public List<TransportationDTO> getrDTO() {
		return rDTO;
	}

	public void setrDTO(List<TransportationDTO> rDTO) {
		this.rDTO = rDTO;
	}
	
	
	
	
}

